<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%String path = request.getContextPath() ;%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c"%>
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>供应商配置维护</title>
<jsp:include page="../../import.jsp" flush="true" />
</head>
<body class="iframeBody">
	<div class="Wrapper">
		<div id="pageInTab" class="pageInTab">
			<form id="formSet1">
				<div class="sqPanel">
					<div class="sqPanel_head inL">
						<div class="btnsBox inLC">
							<span id="spl_rl_add_btn" class="sq-button sq-button-round" onclick="splRlAddBtnEvent();">添加</span>
						</div>
						<span>管理员信息</span>
					</div>
					<div class="sqPanel_body">
						<div id="ligergrid_A1" class="sq-grid"></div>
					</div>
				</div>
				<%--<div class="btnsBox">
					<span id="saveBtn1" class="sq-button sq-button-round">保存</span>
				</div>--%>
			</form>
		</div>
	</div>
<div  style="display:none">
	<div id= "configDilog" >
		<div class="fieldset1 tabel_col tabel_col_1 clearFix">
			<div class="fieldBox col_100_auto">
				<div class="label">事业部</div>
				<div class="field">
					<input  id="buOrgNml" type="text" class="ui-textbox" value="">
				</div>
			</div>
			<div class="fieldBox col_100_auto">
				<div class="label">管理员</div>
				<div class="field">
					<input type="text" id="cstMgrNm" name="cstMgrNm" ltype="combobox" placeholder="请选择"/>
					<input id="mgrId" style="display:none" type="text" class="ui-textbox" value="">
				</div>
				<input id="tel" type="text" style="display:none" class="ui-textbox" value="" validate="{required:true,mobile:true}">
			</div>
			<!-- <div class="fieldBox col_100_auto" style="display:none">
				<div class="label">备注</div>
				<div class="field">
					<input  id="rlRem" type="text" class="ui-textbox"
						value="">
				</div>
			</div> -->
		</div>

	</div>
</div>

<script type="text/javascript">
	var configDialog=null,
		npSocialRlGrid=null,
		searchOrgTree=null,
		mgrManager=null,
		COMBO_GRID1=null,//配置
		buOrgCd=null,//客户经理所属事业部
		org_Cd=null, //客户经理所属组织架构

		splId='${splId}',
        usrIds =${usrIds},
		splBizTypCdList =${splBizTypCdList},
		isPrivateTypCdList = ${isPrivateTypCdList};

	$(function(){
		$("#saveBtn1").click(function(event) {
			var form = liger.get("formSet1");
			if (!form.valid()) {
				$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_MUST_FIX_ITEM_INFO , SQ_type: "warning" });
				return false; //验证不通过，不执行以下代码
			}
			var data = form.getData();
			data["mgrListJSON"] = JSON.stringify(npSocialRlGrid.getData());
			data["splId"]= splId;
			var gridData = eval("(" + data["mgrListJSON"] + ")");
			var str = [];
			for(var i = 0; i < gridData.length; i++){
				if(!gridData[i].isPrivate){
					str.push(gridData[i].rlNm);
				}
			}
			if(str && str.length > 0){
				$.ligerDialog.SQalert({content: "管理员：["+ str+"]私海状态没有设置，请重新设置！" , SQ_type: "warning" });
				return ;
			}
			//alert(JSON.stringify(data));return;
			var url = '<%=path%>/product/splUsrR/addModify.do';
			var wait_dialog;
			$.ajax({
				type: 'post',
				dataType:'json',
				data:data,
				url: url,
				beforeSend: function(){
					  wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING ,SQ_type: "waitting" });
				 },
				success:function(msg){
					 wait_dialog.close();
					 if(msg.success == 1){
						 $.ligerDialog.SQalert({content:msg.info, SQ_type: "success"});
						 npSocialRlGrid.setParm("splId",splId);
						 npSocialRlGrid.reload();
					 }else{
						 $.ligerDialog.SQalert({content:msg.info, SQ_type: "error"});
					 }
				}
			});
		});

		COMBO_GRID1 =$("#cstMgrNm").ligerComboBox({

			selectBoxWidth: 350,
			selectBoxHeight: 240,
			valueFieldID: 'mp',
			valueField: 'id',
			textField: 'refRlNm',
			grid: {
				columns: [
					{ display: '员工姓名', name: 'refRlNm', minWidth: 300 }
				],

				switchPageSizeApplyComboBox: false,
				url : '<%=path%>/system/sysUsr/getUsrByOrgId.do?isDataAuth=0',
				method: 'get',
				root:'rows',
				record:'total',
				pageSize: 10,
				cssClass: "SMALL_grid",
				checkbox: false,
				frozen: false,
				onAfterShowData: function (currentData) {
				}
			},
			delayLoadGrid: true, //点击的时候出发
			onSelected : function(value,text){
				$('#mgrId').val(value);
			}
		});
        COMBO_GRID1.setDisabled();//先禁用 -> 控制下拉表格COMBO_GRID1的请求，启用后点下拉框就会请求
		$('#formSet1').ligerForm({
			validate : true
		});

		initSplConfigGrid();
		initOrgTree();

    });

	//管理员新增dialog
	function splRlAddBtnEvent(){
		if(configDialog==null){
			configDialog = $.ligerDialog.open({
				width: 600,
				height: 250,
				title: '选择管理员',
				target:$('#configDilog'),
				buttons:[{text:'添加',onclick:function(i,d){
						f_add();f_clear();d.hide();
					}},
					{text:'关闭',onclick:function(i,d){
							d.hide();
						}
					}]
			});
		}else{
			configDialog.show();
		}
	};
	function deleteDB(rowdata_id,_id){
		//删除数据库
		var _ids = [];
		_ids.push(_id);
		$.ligerDialog.confirm(LogUtils.LOG_CMM_DELETE_CONFIRM, function (flag) {
			if(flag == false)return;
			var wait_dialog;
			$.ajax({
				type: 'post',
				dataType:'json',
				data:{ids:JSON.stringify(_ids)},
				url: '<%=path%>/product/splUsrR/removeByIds.do',
				beforeSend: function(){
					 wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING,SQ_type:"waitting"});
				},
				success:function(msg){
					wait_dialog.close();
					 if(msg.success == 1){
						 $.ligerDialog.SQalert({content:msg.info, SQ_type: "success"});
						 npSocialRlGrid.reload();
					 }else{
						 $.ligerDialog.SQalert({content:msg.info, SQ_type: "error"});
					 }
				}
			});
		});
	}
	function deletePage(rowdata_id){
		$.ligerDialog.confirm(LogUtils.LOG_CMM_DELETE_CONFIRM, function (flag) {
			if(flag == false)return;
			//删除页面
			npSocialRlGrid.deleteRow(rowdata_id);
		})
	}
	//初始化管理人员信息
	function initSplConfigGrid() {
		if(!splId){//供应商ID为空  新增供应商
			return;
		}
		npSocialRlGrid = $("#ligergrid_A1").ligerGrid({
			url: '<%=path%>/product/splUsrR/searchSplCofigInfo.do?',
			method : 'get',
			parms:{splId:splId},
			async: false,
			columns: [
				{ display: '业务类型', name: 'splBizTypCd', minWidth: 150,
					editor: {
						   type: 'select',
						   data: splBizTypCdList,
						   valueField: 'key',
						   textField: 'value'
					   },
					   valueField: 'splBizTypCd',
					   textField: 'splBizTypCdNm',
					   render: function (item) {
						   return item.splBizTypCdNm;
					   }
				},
				{ display: '事业部', name: 'orgDispNm', width: 300, editor: {type: 'String'}},
				{ display: '管理员', name: 'rlNm', width: 140,editor: {type: 'String'}},
				{ display: '联系方式', name: 'mp', width: 150, editor: {type: 'String'}},
				{ display: '客户经理 id', name: 'usrId', width: 150, hide: true ,editor: {type: 'String'}},
				{ display: 'id', id: 'id', width: 300, hide: true ,editor: {type: 'String'}},
				{ display: '设置私海状态', name: 'isPrivate', minWidth: 150,
					editor: {
						type: 'select',
						data: isPrivateTypCdList,
						valueField: 'key',
						textField: 'value'
					},
					valueField: 'isPrivate',
					textField: 'isPrivateCdNm',
					render: function (item) {
						return item.isPrivateCdNm;
					}
				},
				{ display: '操作', width: 200 , align:'left',
					render: function (rowdata, rowindex, value){
						var jsonData = JSON.stringify(rowdata);
						jsonData = jsonData.replace(/"/g,'@');
						var d='';
						d+='<input type="text" name="id" value="'+rowdata.id+'"/>';
						if(rowdata.id){
							d='<a class="sq-button-hasicon" href="javascript:deleteDB(';
							d+="'"+rowdata.__id+"'";
							d+=",'"+rowdata.id+"'";
							d+=')"><i class="sqBtnIco_7"></i>删除</a>';
							d+='<a id="saveBtn'+rowindex+'" class="sq-button-hasicon" style="display:none;" href="javascript:saveSplUsrR(\''+jsonData+'\','+rowindex+')"><i class="sqBtnIco_10"></i>保存</a>'
						}else{
							d='<a class="sq-button-hasicon" href="javascript:deletePage(';
							d+="'"+rowdata.__id+"'";
							d+=')"><i class="sqBtnIco_7"></i>删除</a>';
							d+='<a id="saveBtn'+rowindex+'" class="sq-button-hasicon" href="javascript:saveSplUsrR(\''+jsonData+'\','+rowindex+')"><i class="sqBtnIco_10"></i>保存</a>'
						}
						return d;
					}
				}
			],
		   usePager: false,
		   root: 'rows',
		   record: 'total',
		   hideLoadButton: true,
		   width: '100%',
		   height: 300,
		   heightDiff: -2,
		   enabledEdit: true,
		   isScroll: false,
		   checkbox: false,
		   rownumbers: true,
		   allowUnSelectRow: true,
		   alternatingRow: false,
		   enabledSort:false,
		   onAfterShowData: function() {
			SetIframe_height ();
		   },
			onAfterEdit : function(editParm){
				$('#saveBtn'+editParm.rowindex).css('display','inline-block');
			}
		});
	};

	function saveSplUsrR(jsonData, index){
		jsonData = jsonData.replace(/@/g, '"');
		var rowdata = JSON.parse(jsonData);
		npSocialRlGrid.submitEdit(rowdata);
		var form = liger.get("formSet1");
		var data = form.getData();
		data["mgrListJSON"] = '['+jsonData+']';
		data["splId"]= splId;
		var gridData = eval("(" + data["mgrListJSON"] + ")");
		data.isAuthCrdChk = $('input[name="isAuthCrdChk"]:checked ').val();
		data.livSwitch = $('input[name="livSwitch"]:checked ').val();
		data.crdLoaSwitch = $('input[name="crdLoaSwitch"]:checked ').val();
		var url = '<%=path%>/product/splUsrR/addOrModifySplUsrR.do';
		var wait_dialog;
		$.ajax({
			type: 'post',
			dataType:'json',
			data:data,
			url: url,
			beforeSend: function(){
				wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING ,SQ_type: "waitting" });
			},
			success:function(msg){
				wait_dialog.close();
				if(msg.success == 1){
					$.ligerDialog.SQalert({content:msg.info, SQ_type: "success"});
					npSocialRlGrid.setParm("splId",splId);
					npSocialRlGrid.reload();
				}else{
					$.ligerDialog.SQalert({content:msg.info, SQ_type: "error"});
				}
			}
		});
	}

	function initOrgTree(){
		var params = {};
		params.cstMgrOrgCd = '${currOrg}';
		searchOrgTree = $("#buOrgNml").ligerComboBox({
			width: 350,
			selectBoxWidth: 350,
			selectBoxHeight: 150,
			//限制只能选叶节点，父节点不能点击
			treeLeafOnly: false,
			textField:"tName",
			valueField:"tCode",
			showTreePath: true,
			tree: {
				ajaxType: 'get',
				textFieldName:"tName",
				idFieldName:"tCode",
				checkbox: false,
				parentIcon: "none",
				childIcon: "none",
				needCancel : false,
				single : true
			},
			onSelected : function(value,text){
				var a=searchOrgTree.treeManager.getDataByID(value)
				searchMgrNmList(a.id);
				buOrgCd=value;//客户经理所属事业部
				org_Cd=a.id; //客户经理所属组织架构
			}
		});
		$.ajax({
			type: 'get',
			dataType:'json',
			data: params,
			url: '<%=path%>/customer/cstbscinfo/initOrgTree.do',
			success:function(msg){
				searchOrgTree.treeManager.setData(msg);
				searchOrgTree.clear();
			}
		});
	}
	function f_add(){
		if(buOrgCd != null && buOrgCd.length > 8){
			if(null == $('#mgrId').val() || "" == $('#mgrId').val()){
				$.ligerDialog.SQalert({content: "请选择管理员！" ,SQ_type: "warning" });
			}else{
				var flag = 0;
				for(var i = 0; i < usrIds.length; i++){
					if(usrIds[i] == $('#mgrId').val()){
						flag = 1;
					}
				}
				if(flag == 0){
					var row = npSocialRlGrid.getRow(0);
					npSocialRlGrid.addRow({
						splBizTypCd: "",
						orgDispNm: $('#buOrgNml').val(),
						rlNm: $('#cstMgrNm').val(),
						mp: $('#tel').val(),
						usrId : $('#mgrId').val(),
						buOrgCd:buOrgCd, //客户经理所属事业部
						org_Cd:org_Cd,//客户经理所属组织架构
						id:""
					},row,true);
					usrIds.push($('#mgrId').val());
				}else{
					$.ligerDialog.SQalert({content: "与该用户已存在业务，请勿重复添加!" ,SQ_type: "warning" });
				}
			}
		}else{
			$.ligerDialog.SQalert({content: "请配置到具体分公司下!" ,SQ_type: "warning" });
		}
	}
	function f_clear(){
		$('#splBizTypCd').val('');
		var cstMgrIdObj = liger.get("cstMgrNm");
		cstMgrIdObj.selectValue(" ");
		$('#tel').val('');
		$('#mgrId').val('');
	}
	function searchMgrNmList(orgId){
		COMBO_GRID1.setEnabled();//启用//先禁用 -> 控制下拉表格COMBO_GRID1的请求，启用后点下拉框就会请求
		$(COMBO_GRID1.element).click();
		var paramData = {"orgId":orgId};
		liger.get('cstMgrNm').gridManager.setParm('orgId',orgId);
		liger.get('cstMgrNm').gridManager.reload();
	}
</script>
</body>
</html>
